﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RI
{
    public partial class CurrentUser : Form
    {
        /// <summary>
        /// 当前网站用户列表
        /// </summary>
        private DataTable dtUserList = new DataTable();

        public CurrentUser()
        {
            InitializeComponent();

            SetCursor();
        }

        /// <summary>
        /// 设定当前光标
        /// </summary>
        private void SetCursor()
        {
            //using (SystemConfig sc = new SystemConfig())
            //{
            //    this.Cursor = sc.SetCurrentCursor("Skins/Arrow.ani", this.Cursor);
            //    for (int i = 0; i < tabControl1.TabCount; i++)
            //    {
            //        foreach (Control co in tabControl1.TabPages[i].Controls)
            //        {
            //            if (co is TextBox || co is RichTextBox)
            //            {
            //                co.Cursor = sc.SetCurrentCursor("Skins/IBeam.ani", co.Cursor);
            //            }
            //        }
            //    }
            //}
        }

        private void CurrentUser_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        private void cbxUserList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxUserList.Items.Count != 0)
            {
                string[] uid = cbxUserList.Text.Split('|');
                for (int i = 0; i < dtUserList.Rows.Count; i++)
                {
                    if (dtUserList.Rows[i][0].ToString() == uid[0].ToString())
                    {
                        txtUserID.Text = dtUserList.Rows[i][0].ToString();
                        txtUserName.Text = dtUserList.Rows[i][2].ToString();
                        txtPassword.Text = dtUserList.Rows[i][3].ToString();
                        txtShop.Text = dtUserList.Rows[i][4].ToString();
                        rtxUser_Remark.Text = dtUserList.Rows[i][5].ToString();
                    }
                }
            }
        }
        //修改用户
        private void btnUP_UserInfo_Click(object sender, EventArgs e)
        {
            string id = txtUserID.Text.Trim();
            string username = txtUserName.Text.Trim();
            string psd = txtPassword.Text.Trim();
            string shop = txtShop.Text.Trim();
            string remark = rtxUser_Remark.Text.Trim();
            using (Business bus = new Business())
            {
                bus.ModifyUser(id, shop, username, psd, remark);
            }

            BindIDSite(txtSiteID.Text.Trim());
        }
        //修改网站
        private void btnUP_Site_Click(object sender, EventArgs e)
        {
            string id = txtSiteID.Text.Trim();
            string title = txtTitle.Text.Trim();
            string url = txtDomain.Text.Trim();
            string remark = rtxSite_Remark.Text.Trim();
            string is_reg = "true";
            string is_del = "true";
            string is_sucess = "true";
            if (chk_ISRegister.Checked != true) is_reg = "false";
            if (chk_ISLaHei.Checked != true) is_del = "false";
            if (chk_ISSucess.Checked != true) is_sucess = "false";
            
            using (Business bus = new Business())
            {
                bus.ModiftSite(id, title, url, remark, is_reg, is_del, is_sucess);
            }

            BindIDSite(id);
        }

        /// <summary>
        /// 绑定指定ID网站
        /// </summary>
        /// <param name="id">编号</param>
        public void BindIDSite(string id)
        {
            DataTable dt = new DataTable();
            dt = null;
            using (Business bus = new Business())
            {
                dt = bus.GetIDSite(id);
            }
            if (dt.Rows.Count != 0)
            {
                txtSiteID.Text = dt.Rows[0][0].ToString();
                txtDomain.Text = dt.Rows[0][1].ToString();
                txtTitle.Text = dt.Rows[0][2].ToString();
                txtCreateDate.Text = dt.Rows[0][3].ToString();
                rtxSite_Remark.Text = dt.Rows[0][4].ToString();
                if (dt.Rows[0][5].ToString().ToUpper().Equals("FALSE"))
                    chk_ISRegister.Checked = false;
                else
                    chk_ISRegister.Checked = true;

                if (dt.Rows[0][6].ToString().ToUpper().Equals("FALSE"))
                    chk_ISLaHei.Checked = false;
                else
                    chk_ISLaHei.Checked = true;

                if (dt.Rows[0][7].ToString().ToUpper().Equals("FALSE"))
                    chk_ISSucess.Checked = false;
                else
                    chk_ISSucess.Checked = true;

                //绑定当前网站下的所有用户
                BindItemUser(id);
            }
            else
            {
                cbxUserList.Items.Clear();
                for (int i = 0; i < tabControl1.TabCount; i++)
                {
                    foreach (Control co in tabControl1.TabPages[i].Controls)
                    {
                        if (co is TextBox || co is RichTextBox)
                        {
                            co.Text = string.Empty;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 获取指定网站下的所有已注册用户
        /// </summary>
        /// <param name="sid">网站ID</param>
        private void BindItemUser(string sid)
        {
            dtUserList.Clear();
            using (Business bus = new Business())
            {
                dtUserList = bus.GetItemCatUser(sid);
            }
            if (dtUserList.Rows.Count != 0)
            {
                cbxUserList.Items.Clear();
                for (int i = 0; i < dtUserList.Rows.Count; i++)
                {
                    string item = string.Format("{0}|{1}",
                        dtUserList.Rows[i][0].ToString(),
                        dtUserList.Rows[i][2].ToString());
                    cbxUserList.Items.Add(item);
                }
                cbxUserList.SelectedIndex = 0;
                txtUserID.Text = dtUserList.Rows[0][0].ToString();
                txtUserName.Text = dtUserList.Rows[0][2].ToString();
                txtPassword.Text = dtUserList.Rows[0][3].ToString();
                txtShop.Text = dtUserList.Rows[0][4].ToString();
                rtxUser_Remark.Text = dtUserList.Rows[0][5].ToString();
            }
            else
            {
                cbxUserList.Items.Clear();
                foreach (Control co in tabPage1.Controls)
                {
                    if (co is TextBox || co is RichTextBox)
                    {
                        co.Text = string.Empty;
                    }
                }
            }
        }
        
    }
}
